package keye.document_manage.gui;

import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingWorker;

import keye.document_manage.entity.SelectParameters;
import keye.document_manage.service.IPushService;
import keye.document_manage.util.ConvertStringUtil;

public class PushDataView extends SwingWorker<String, String>{

	private ProgressFrame progressFrame;
	private IPushService pushService;
	private List<SelectParameters> selectParas;
	private CheckNode checkNode;
	private boolean isCopyFiles;
	private String osPosition;
	private String  newPath;
	private JButton pushBtn;
	
	public PushDataView() {
	}
	
	public PushDataView(ProgressFrame progressFrame, JButton pushBtn, IPushService pushService,
			List<SelectParameters> selectParas, CheckNode checkNode,boolean isCopyFiles,
			String osPosition, String newPath) {
		this.progressFrame = progressFrame;
		this.pushService = pushService;
		this.selectParas = selectParas;
		this.checkNode = checkNode;
		this.isCopyFiles = isCopyFiles;
		this.osPosition = osPosition==null?"":osPosition;
		this.newPath = newPath;
		this.pushBtn = pushBtn;
	}

	@Override
	protected String doInBackground() throws Exception {
		pushBtn.setEnabled(false);
		progressFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		long startTime=System.currentTimeMillis();   //获取开始时间
		pushService.pushDatas(progressFrame,selectParas, checkNode, isCopyFiles, osPosition,newPath);
		long endTime=System.currentTimeMillis(); //获取结束时间
		return ConvertStringUtil.convertTime(endTime-startTime);
	}

	@Override
	protected void done() {
		try {
			progressFrame.addOneComplete(checkNode.getFirstLeaf().getUserObject().toString(), get(),newPath);
			pushBtn.setEnabled(true);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		progressFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	}

}
